package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineTakeDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineTakeDetailDAO;

import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.MedicineTake;
import cn.edu.scau.cmi.hombio.domain.MedicineTakeDetail;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for MedicineTakeDetail entities
 * 
 */

@Service("MedicineTakeDetailService")

@Transactional
public class MedicineTakeDetailServiceImpl implements MedicineTakeDetailService {

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages MedicineTake entities
	 * 
	 */
	@Autowired
	private MedicineTakeDAO medicineTakeDAO;

	/**
	 * DAO injected by Spring that manages MedicineTakeDetail entities
	 * 
	 */
	@Autowired
	private MedicineTakeDetailDAO medicineTakeDetailDAO;

	/**
	 * Instantiates a new MedicineTakeDetailServiceImpl.
	 *
	 */
	public MedicineTakeDetailServiceImpl() {
	}

	/**
	 * Save an existing MedicineTakeDetail entity
	 * 
	 */
	@Transactional
	public void saveMedicineTakeDetail(MedicineTakeDetail medicinetakedetail) {
		MedicineTakeDetail existingMedicineTakeDetail = medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(medicinetakedetail.getId());

		if (existingMedicineTakeDetail != null) {
			if (existingMedicineTakeDetail != medicinetakedetail) {
				existingMedicineTakeDetail.setId(medicinetakedetail.getId());
				existingMedicineTakeDetail.setTraceCode(medicinetakedetail.getTraceCode());
				existingMedicineTakeDetail.setBoxTraceCode(medicinetakedetail.getBoxTraceCode());
				existingMedicineTakeDetail.setCaseTraceCode(medicinetakedetail.getCaseTraceCode());
				existingMedicineTakeDetail.setIsBox(medicinetakedetail.getIsBox());
			}
			medicinetakedetail = medicineTakeDetailDAO.store(existingMedicineTakeDetail);
		} else {
			medicinetakedetail = medicineTakeDetailDAO.store(medicinetakedetail);
		}
		medicineTakeDetailDAO.flush();
	}

	/**
	 * Return a count of all MedicineTakeDetail entity
	 * 
	 */
	@Transactional
	public Integer countMedicineTakeDetails() {
		return ((Long) medicineTakeDetailDAO.createQuerySingleResult("select count(o) from MedicineTakeDetail o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public MedicineTakeDetail findMedicineTakeDetailByPrimaryKey(Integer id) {
		return medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(id);
	}

	/**
	 * Delete an existing MedicineTake entity
	 * 
	 */
	@Transactional
	public MedicineTakeDetail deleteMedicineTakeDetailMedicineTake(Integer medicinetakedetail_id, Integer related_medicinetake_id) {
		MedicineTakeDetail medicinetakedetail = medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(medicinetakedetail_id, -1, -1);
		MedicineTake related_medicinetake = medicineTakeDAO.findMedicineTakeByPrimaryKey(related_medicinetake_id, -1, -1);

		medicinetakedetail.setMedicineTake(null);
		related_medicinetake.getMedicineTakeDetails().remove(medicinetakedetail);
		medicinetakedetail = medicineTakeDetailDAO.store(medicinetakedetail);
		medicineTakeDetailDAO.flush();

		related_medicinetake = medicineTakeDAO.store(related_medicinetake);
		medicineTakeDAO.flush();

		medicineTakeDAO.remove(related_medicinetake);
		medicineTakeDAO.flush();

		return medicinetakedetail;
	}

	/**
	 * Delete an existing Medicine entity
	 * 
	 */
	@Transactional
	public MedicineTakeDetail deleteMedicineTakeDetailMedicine(Integer medicinetakedetail_id, Integer related_medicine_id) {
		MedicineTakeDetail medicinetakedetail = medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(medicinetakedetail_id, -1, -1);
		Medicine related_medicine = medicineDAO.findMedicineByPrimaryKey(related_medicine_id, -1, -1);

		medicinetakedetail.setMedicine(null);
		related_medicine.getMedicineTakeDetails().remove(medicinetakedetail);
		medicinetakedetail = medicineTakeDetailDAO.store(medicinetakedetail);
		medicineTakeDetailDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		medicineDAO.remove(related_medicine);
		medicineDAO.flush();

		return medicinetakedetail;
	}

	/**
	 * Return all MedicineTakeDetail entity
	 * 
	 */
	@Transactional
	public List<MedicineTakeDetail> findAllMedicineTakeDetails(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<MedicineTakeDetail>(medicineTakeDetailDAO.findAllMedicineTakeDetails(startResult, maxRows));
	}

	/**
	 * Save an existing MedicineTake entity
	 * 
	 */
	@Transactional
	public MedicineTakeDetail saveMedicineTakeDetailMedicineTake(Integer id, MedicineTake related_medicinetake) {
		MedicineTakeDetail medicinetakedetail = medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(id, -1, -1);
		MedicineTake existingmedicineTake = medicineTakeDAO.findMedicineTakeByPrimaryKey(related_medicinetake.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicineTake != null) {
			existingmedicineTake.setId(related_medicinetake.getId());
			existingmedicineTake.setTime(related_medicinetake.getTime());
			existingmedicineTake.setMemo(related_medicinetake.getMemo());
			related_medicinetake = existingmedicineTake;
		} else {
			related_medicinetake = medicineTakeDAO.store(related_medicinetake);
			medicineTakeDAO.flush();
		}

		medicinetakedetail.setMedicineTake(related_medicinetake);
		related_medicinetake.getMedicineTakeDetails().add(medicinetakedetail);
		medicinetakedetail = medicineTakeDetailDAO.store(medicinetakedetail);
		medicineTakeDetailDAO.flush();

		related_medicinetake = medicineTakeDAO.store(related_medicinetake);
		medicineTakeDAO.flush();

		return medicinetakedetail;
	}

	/**
	 * Delete an existing MedicineTakeDetail entity
	 * 
	 */
	@Transactional
	public void deleteMedicineTakeDetail(MedicineTakeDetail medicinetakedetail) {
		medicineTakeDetailDAO.remove(medicinetakedetail);
		medicineTakeDetailDAO.flush();
	}

	/**
	 * Load an existing MedicineTakeDetail entity
	 * 
	 */
	@Transactional
	public Set<MedicineTakeDetail> loadMedicineTakeDetails() {
		return medicineTakeDetailDAO.findAllMedicineTakeDetails();
	}

	/**
	 * Save an existing Medicine entity
	 * 
	 */
	@Transactional
	public MedicineTakeDetail saveMedicineTakeDetailMedicine(Integer id, Medicine related_medicine) {
		MedicineTakeDetail medicinetakedetail = medicineTakeDetailDAO.findMedicineTakeDetailByPrimaryKey(id, -1, -1);
		Medicine existingmedicine = medicineDAO.findMedicineByPrimaryKey(related_medicine.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicine != null) {
			existingmedicine.setId(related_medicine.getId());
			existingmedicine.setName(related_medicine.getName());
			existingmedicine.setNumber(related_medicine.getNumber());
			existingmedicine.setCompany(related_medicine.getCompany());
			existingmedicine.setQuantityPerCase(related_medicine.getQuantityPerCase());
			existingmedicine.setQuantityPerBox(related_medicine.getQuantityPerBox());
			existingmedicine.setPhone(related_medicine.getPhone());
			existingmedicine.setMemo(related_medicine.getMemo());
			related_medicine = existingmedicine;
		}

		medicinetakedetail.setMedicine(related_medicine);
		related_medicine.getMedicineTakeDetails().add(medicinetakedetail);
		medicinetakedetail = medicineTakeDetailDAO.store(medicinetakedetail);
		medicineTakeDetailDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		return medicinetakedetail;
	}
}
